//
//  GoodsDetailTableViewCell.m
//  Consume100
//
//  Created by eliot on 16/3/29.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "GoodsDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation GoodsDetailTableViewCell
- (void)setupCellWithDic:(NSDictionary *)dic {
    
    NSArray *arr = dic[@"shopProductJson"];
    NSArray *arr2 = dic[@"shopProductImgJson"];
    NSDictionary *dataDic = arr.firstObject;
    
    
    NSLog(@"%@", dataDic[@"img"]);
    
    if (dataDic[@"img"]) {
        NSMutableArray *imgArr = [NSMutableArray array];
        NSDictionary *tempDic = [NSDictionary dictionaryWithObject:dataDic[@"img"] forKey:@"img"];
        [imgArr addObject:tempDic];
        [imgArr addObjectsFromArray:arr2];
        //初始化数组,将图片压入数组
        self.imageArray = [NSMutableArray arrayWithArray:imgArr];
    }
   
    
    
    //    UIScrollView *tempView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Width, Width)];
    //    self.scrollView = tempView;
    self.scrollViewH.constant = Width;
    self.scrollViewW.constant = Width;
    self.titleLb.text = dataDic[@"name"];
    self.descLb.text = dataDic[@"putParameter"];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //创建页点
    UIPageControl *tempPage = [[UIPageControl alloc]initWithFrame:CGRectMake(0, Width - 20, self.frame.size.width,20)];
    
    [tempPage setBackgroundColor:[UIColor clearColor]];
    [tempPage setAlpha:1];
    self.pageControl = tempPage;
    //[tempPage release];
    
    
    //将滚动视图和翻页视图添加到视图中
    [self addSubview:self.pageControl];
    
    [self createsCrollView:nil];
}
- (void)awakeFromNib {

}

#pragma ------创建视图区域----------

//创建滚动视图区域

- (void)createsCrollView:(id)sender {
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    //设置委托
    self.scrollView.delegate = self;
    //将是否取消对内容视图触摸的反应设为NO
    self.scrollView.canCancelContentTouches = NO;
    //设置滚动条类型
    // self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    //是否可以缩放
    self.scrollView.scrollEnabled = YES;
    //是否可以进行页面切换
    self.scrollView.pagingEnabled = YES;
    NSUInteger page = 0;//为了记录页数
    int originX = 0;
    //填充滑动视图内容
    for (NSDictionary *imageDic in self.imageArray) {
        //首先创建一个视图
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectZero];
        imageview.contentMode = UIViewContentModeScaleAspectFit;
        //为这个图片设置适当位置
        CGRect rect ;
        rect.origin.x = originX;
        rect.origin.y = 0;
        rect.size.width = Width;
        rect.size.height = Width;
        imageview.frame = rect;
        //设置图片为背景
        [imageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.188:8080%@", imageDic[@"img"]]]];
        //添加到视图中
        [self.scrollView addSubview:imageview];
        //确定下张图片开始的x坐标
        originX += Width;
        //记录页数
        page++;
        
        NSLog(@"%f, %f", self.scrollView.frame.size.width, imageview.frame.size.width);
    }
    //设置总页数
    self.pageControl.numberOfPages = page;
    //设置默认页为首页
    self.pageControl.currentPage = 0;
    self.pageControl.tag = 100;
    //为页数控制关联方法
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    //显示适当区域
    [self.scrollView setContentSize:CGSizeMake(originX, self.scrollView.frame.size.height)];
}


- (void)repartScroll {
    if (self.pageControl.currentPage == 3) {
        self.pageControl.currentPage = 0;
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }else{
        self.pageControl.currentPage = self.pageControl.currentPage + 1;
    }
    CGRect rect;
    rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;
    rect.origin.y = 0;
    rect.size.width = self.scrollView.frame.size.width;
    rect.size.height = self.scrollView.frame.size.height;
    //设置滚动视图的可视区域
    [self.scrollView scrollRectToVisible:rect animated:YES];
    
}

//点击小点切换页面
//
- (void)changePage:(id)sender {
    //创建一个区域
    CGRect rect;
    rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;
    rect.origin.y = 0;
    rect.size.width = self.scrollView.frame.size.width;
    rect.size.height = self.scrollView.frame.size.height;
    //设置滚动视图的可视区域
    [self.scrollView scrollRectToVisible:rect animated:YES];
    
}

//重写滑动结束后的反应，就是把pageConteol的当前页跟随滑动而动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //取滚动视图区域的宽
    float orginX = self.scrollView.frame.size.width;
    //利用当前视图显示区域到view.frame的偏移量，来换算获取当前处在第几页
    int page = ((self.scrollView.contentOffset.x - orginX))/orginX +1;
    //当前页改为这一页。
    self.pageControl.currentPage = page;
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
